float4x4 World;
float4x4 View;
float4x4 Projection;

float highest;
float lowest;
float worldWidth;
float worldHeight;

texture2D heightmap;
sampler2D heightmap_sampler = sampler_state
{
	Texture = heightmap;  
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = POINT;
	AddressU = Wrap;
	AddressV = Wrap;
};

texture2D normalMap;
sampler2D normalMap_sampler = sampler_state
{
	Texture = normalMap;
	MinFilter = POINT;
	MagFilter = POINT;
	MipFilter = POINT;
	AddressU = Wrap;
	AddressV = Wrap;
};

float3 lightDirection;

//sampler2D tex1 : register(s2);
//sampler2D tex2 : register(s3);
//sampler2D tex3 : register(s4);
//sampler2D tex4 : register(s5);
//sampler2D tex5 : register(s6);
//sampler2D tex6 : register(s7);


float4 sampleTextureIndex(float2 texCoord, float4 weight1, float4 weight2)
{
	//float4 c1 = tex2D(tex1, texCoord) * weight1.g;
	//float4 c2 = tex2D(tex2, texCoord) * weight1.b;
	//float4 c3 = tex2D(tex3, texCoord) * weight1.a;

	//float4 c4 = tex2D(tex4, texCoord) * weight2.r;
	//float4 c5 = tex2D(tex5, texCoord) * weight2.g;
	//float4 c6 = tex2D(tex6, texCoord) * weight2.b;

	//return c1 + c2 + c3 + c4 + c5 + c6;
	return float4(.5f, .5f, .5f, 1.f);
}


struct VertexShaderInput
{
    float3 Position : POSITION0;
	float3 normal	: NORMAL0;
	float2 uv		: TEXCOORD0;
	float4 texWeights1 : TEXCOORD1;
	float4 texWeights2 : TEXCOORD2;
};

struct VertexShaderOutput
{
    float4 Position : POSITION0;
	float4 worldPosition : COLOR0;
	float3 normal	: NORMAL;
	float2 uv		: TEXCOORD;
	float4 texWeights1 : TEXCOORD1;
	float4 texWeights2 : TEXCOORD2;
};

struct ps_in
{
	float4 position : COLOR0;
	float2 uv		: TEXCOORD0;
	float3 normal	: NORMAL;
	float4 texWeights1 : TEXCOORD1;
	float4 texWeights2 : TEXCOORD2;
};

VertexShaderOutput VertexShaderFunction(VertexShaderInput input)
{
    VertexShaderOutput output;

	float4 worldPosition = mul(float4(input.Position, 1.f), World);
		float4 geometryMapPosition = float4(input.Position.x / worldWidth, input.Position.z / worldHeight, 0.f, 0.f);
		float heightmapVal = tex2Dlod(heightmap_sampler, geometryMapPosition).a * (highest - lowest);
	float3 normal = tex2Dlod(normalMap_sampler, geometryMapPosition);
	worldPosition.y = lowest + heightmapVal;
	output.normal = normal;
    float4 viewPosition = mul(worldPosition, View);
    output.Position= mul(viewPosition, Projection);
	output.worldPosition = float4(input.Position, 1.f);

	output.texWeights1 = input.texWeights1;
	output.texWeights2 = input.texWeights2;
	output.uv = input.uv;

    return output;
}

float4 PixelShaderFunction(ps_in input) : COLOR0
{
	float lambert = saturate(dot(-lightDirection, input.normal));

	float4 diffuse = sampleTextureIndex(input.uv, input.texWeights1, input.texWeights2);


	//	return input.texWeights1;
	return diffuse * lambert;
}

technique Technique1
{
    pass Pass1
    {
        // TODO: set renderstates here.

        VertexShader = compile vs_3_0 VertexShaderFunction();
        PixelShader = compile ps_3_0 PixelShaderFunction();
    }
}
